package com.kede.api.file.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kede.api.file.domain.ResourceFileDomain;
import com.kede.api.file.service.ResourceFileService;
import com.kede.api.file.mapper.ResourceFileMapper;
import com.kede.api.file.utils.FileUtil;
import com.kede.dictenum.BoolDictEnum;
import com.kede.dictenum.ResourceFunDictEnum;
import com.kede.pojo.ResHttp;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 11856
* @description 针对表【resource_file】的数据库操作Service实现
* @createDate 2023-05-26 15:00:34
*/
@Service
@Slf4j
public class ResourceFileServiceImpl extends ServiceImpl<ResourceFileMapper, ResourceFileDomain>
    implements ResourceFileService{

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @Value("${file.location}")
    private String localtion;
    public static String uploadPrefix = "uploads";

    @Override
    public List<String> upload(MultipartFile[] uploadFiles) {
        List list = new ArrayList();//存储生成的访问路径
        if (uploadFiles.length > 0) {
            for (int i = 0; i < uploadFiles.length; i++) {
                MultipartFile uploadFile = uploadFiles[i];
                String dirName = uploadPrefix + File.separatorChar+ sdf.format(new Date());
                File folder = new File(localtion +File.separatorChar+ dirName);
                if (!folder.isDirectory()) {
                    folder.mkdirs();
                    try {
                        Runtime.getRuntime().exec("chmod 777 "+folder.getPath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                String oldName = uploadFile.getOriginalFilename();
                log.debug("oldName=={}",oldName);
                try {
                    //保存文件
                    uploadFile.transferTo(new File(folder, oldName));
                    list.add(File.separatorChar + dirName+File.separatorChar+oldName);
//                    保存到数据库
                    ResourceFileDomain resourceFileDomain = new ResourceFileDomain();
                    resourceFileDomain.setFunction(ResourceFunDictEnum.good.getCode());
                    resourceFileDomain.setBucket("");
                    resourceFileDomain.setCreateBy("api-file");
                    resourceFileDomain.setUpdateBy("api-file");
                    resourceFileDomain.setIslocal(BoolDictEnum.TRUE.getCode());
                    resourceFileDomain.setLink("");
                    resourceFileDomain.setName(oldName);
                    resourceFileDomain.setPath(dirName);
                    resourceFileDomain.setRealName(uploadFile.getOriginalFilename());
                    resourceFileDomain.setSize(FileUtil.readableFileSize(uploadFile.getSize()));
                    String suffix = FileUtil.getSuffix(uploadFile.getOriginalFilename());
                    resourceFileDomain.setSuffix(suffix);
                    resourceFileDomain.setType(FileUtil.getFileType(suffix));
                    save(resourceFileDomain);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return list;
        } else if (uploadFiles.length == 0) {
            throw  new RuntimeException("请选择文件");
        }else {
            throw  new RuntimeException("上传文件出错");
        }
    }

    @Override
    public WangEditorUploadRes wangEditorUpload(MultipartFile[] uploadFiles,String hrefPrefix) {
        List<String> upload = upload(uploadFiles);
        String url = hrefPrefix + upload.get(0);
        WangEditorUploadData wangEditorUploadData = new WangEditorUploadData();
        wangEditorUploadData.setUrl(url);
        return WangEditorUploadRes.okData(wangEditorUploadData);
    }


    @Data
    @AllArgsConstructor
    public static class WangEditorUploadRes{

        Integer errno;
        WangEditorUploadData data;
        String message;

        static WangEditorUploadRes fail(){
            return new WangEditorUploadRes(1,null,"上传失败");
        }

        static WangEditorUploadRes okData(WangEditorUploadData wangEditorUploadData){
            return new WangEditorUploadRes(0,wangEditorUploadData,"上传成功");
        }

    }

    @Data
    static class WangEditorUploadData{

        String url;

        String alt;

        String href;
    }
}




